package com.example.administrator.youlu.fragment;


import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;

import com.example.administrator.youlu.R;
import com.example.administrator.youlu.adapter.MyBaseAdapter;
import com.example.administrator.youlu.adapter.MyContactsAdapter;
import com.example.administrator.youlu.been.Contact;
import com.example.administrator.youlu.manager.ContactManager;
import com.example.administrator.youlu.manager.DialogManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class CaotactFragment extends BaseFragment {


    @BindView(R.id.actionBar)
    LinearLayout actionBar;
    Unbinder unbinder;
    @BindView(R.id.gridView)
    GridView gridView;
    private List<Contact> mData = new ArrayList<>();
    MyContactsAdapter adapter;

    public CaotactFragment() {
        // Required empty public constructor
    }

    boolean isFlay=true;


    @RequiresApi(api = Build.VERSION_CODES.ECLAIR)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_caotact, container, false);
        unbinder = ButterKnife.bind(this, view);
        adapter=new MyContactsAdapter(getContext());
        adapter.addDatas(mData,true);
        gridView.setAdapter(adapter);

        //gridView注册监听
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (i==0){
                    DialogManager.AddContactDialog(getContext());
                }else {
                    DialogManager.upDateContactDialog(getContext(),adapter.getItem(i));
                }

            }
        });
//        gridView

        return view;
    }

    @Override
    public void intiView() {
        setActionBar(-1, "联系人", -1);

    }

    private void setmData(){
        mData.clear();
        ContentResolver cr = getActivity().getContentResolver();

        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);
        while (cursor.moveToNext()) {

            Contact contact = new Contact();
            String _Id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            int photo_id = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_ID));
            contact.set_id(_Id);
            Cursor cursor2 = cr.query(ContactsContract.RawContacts.CONTENT_URI,
                    new String[]{ContactsContract.RawContacts._ID},
                    ContactsContract.RawContacts.CONTACT_ID + "=" + _Id,
                    null, null);
            String raw_contact = null;
            while (cursor2.moveToNext()) {
                raw_contact = cursor2.getString(cursor2.getColumnIndex(ContactsContract.RawContacts._ID));
                contact.setRaw_contact_id(raw_contact);

            }
            cursor2.close();

            Cursor cursor3 = cr.query(ContactsContract.Data.CONTENT_URI,
                    new String[]{ContactsContract.Data.RAW_CONTACT_ID,
                            ContactsContract.Data.MIMETYPE,
                            ContactsContract.Data.DATA1,
                            ContactsContract.Data.DATA15},
                    ContactsContract.Data.RAW_CONTACT_ID + "=" + raw_contact,
                    null, null);
            while (cursor3.moveToNext()) {
                String mimatype = cursor3.getString(cursor3.getColumnIndex(ContactsContract.Data.MIMETYPE));
                if (mimatype.equals(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)) {
                    String name = cursor3.getString(cursor3.getColumnIndex(ContactsContract.Data.DATA1));
                    contact.setName(name);
                } else if (mimatype.equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
                    String phone = cursor3.getString(cursor3.getColumnIndex(ContactsContract.Data.DATA1));
                    contact.setPhone(phone);
                }else if (mimatype.equals(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)){
                    String email = cursor3.getString(cursor3.getColumnIndex(ContactsContract.Data.DATA1));
                    contact.setEmail(email);
                }else if (mimatype.equals(ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)){
                    String email = cursor3.getString(cursor3.getColumnIndex(ContactsContract.Data.DATA1));
                    contact.setAdress(email);
                }
            }
            cursor3.close();

            Cursor cursor4 = cr.query(ContactsContract.Data.CONTENT_URI,
                    new String[]{ContactsContract.Data.DATA15},
                    ContactsContract.Data._ID + "=" + photo_id,
                    null, null);

            if (cursor4.moveToNext()){
                Bitmap bitmap=null;
                byte[] bytes=cursor4.getBlob(cursor4.getColumnIndex(ContactsContract.Data.DATA15));
                bitmap=BitmapFactory.decodeByteArray(bytes,0,bytes.length);
                contact.setPhoto(bitmap);

            }
            cursor4.close();


            mData.add(contact);

        }
        cursor.close();

        mData.add(0,new Contact("0","添加联系人"));


        if (adapter!=null)
            adapter.addDatas(mData,true);

    }

    @Override
    public void onResume() {
        super.onResume();
            setmData();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
